package ce.com.newbluedemo.k2;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import ce.com.cenewbluesdk.entity.k2.K2_SendDevSettingStruct;
import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEDevK2Proxy;
import ce.com.newbluedemo.R;

/**
 * Created by YaoBinkun on 2016/12/28.
 */

public class ActivitySendSettings extends K2BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private byte mClockType;
    private byte mCallEnable;
    private byte mMsgEnable;
    private byte mAlarmEnable;
    private byte mLowpowerEnable;
    private byte mHeartEnable;
    private byte mWatchFaceType;

    public static final byte HEXADECIMAL_12 = 0;
    public static final byte HEXADECIMAL_24 = 1;

    public static final byte NOT_SET = (byte) 0xff;

    public static final byte SWTCH_CLOSE = 0;
    public static final byte SWTCH_OPEN = 1;

    private CEDevK2Proxy k2Proxy;

    private RadioGroup RG1, RG2, RG3, RG4, RG5, RG6;

    private EditText mEtWatchface;

    private Handler mK2Handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0){
                showToast("发送失败，请稍后重试！");
            } else {
                RG1.clearCheck();
                RG2.clearCheck();
                RG3.clearCheck();
                RG4.clearCheck();
                RG5.clearCheck();
                RG6.clearCheck();
                mEtWatchface.setText("");
                showToast("发送成功！");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_settings);
        k2Proxy = BleFactory.getInstance().getK2Proxy();
        k2Proxy.setHandler(mK2Handler);
        RG1 = ((RadioGroup) findViewById(R.id.rg_clock_disp_type));
        RG1.setOnCheckedChangeListener(this);
        RG2 = ((RadioGroup) findViewById(R.id.rg_motor_call_enable));
        RG2.setOnCheckedChangeListener(this);
        RG3 = ((RadioGroup) findViewById(R.id.rg_motor_message_enable));
        RG3.setOnCheckedChangeListener(this);
        RG4 = ((RadioGroup) findViewById(R.id.rg_motor_alarm_enable));
        RG4.setOnCheckedChangeListener(this);
        RG5 = ((RadioGroup) findViewById(R.id.rg_motor_lowpower_enable));
        RG5.setOnCheckedChangeListener(this);
        RG6 = ((RadioGroup) findViewById(R.id.rg_heart_enable));
        RG6.setOnCheckedChangeListener(this);
        mEtWatchface = (EditText) findViewById(R.id.et_watch_face);
        mEtWatchface.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) return;
                String input = mEtWatchface.getText().toString();
                if (TextUtils.isEmpty(input)){
                    showToast("输入不能为空");
                }else {
                    if (!LittleUtils.isInteger(input) ||
                            0 > Integer.parseInt(input) ||
                            127 < Integer.parseInt(input)){//TODO  蓝牙协议里定的表盘序号是0到254，然而 K2_SendDevSettingStruct的构造方法里对应参数是byte类型
                        showToast("输入不正确，请重新输入");
                        return;
                    }
                    mWatchFaceType = (byte) Integer.parseInt(input);
                }
            }
        });
        findViewById(R.id.btn_send_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!k2Proxy.isConnectOK()) {
                    showToast("蓝牙已经断开");
                    return;
                }
                if (RG1.getCheckedRadioButtonId() == -1 ||
                        RG2.getCheckedRadioButtonId() == -1 ||
                        RG3.getCheckedRadioButtonId() == -1 ||
                        RG4.getCheckedRadioButtonId() == -1 ||
                        RG5.getCheckedRadioButtonId() == -1 ||
                        RG6.getCheckedRadioButtonId() == -1) {
                    showToast("输入有误，请检查输入后再重试！");
                    return;
                }

                k2Proxy.getSendHelper().sendSetting(new K2_SendDevSettingStruct(mClockType,
                        mMsgEnable, mAlarmEnable, mLowpowerEnable, mWatchFaceType, mHeartEnable));

            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getId()) {
            case R.id.rg_clock_disp_type:
                mClockType = (checkedId == R.id.rb_clock_open) ? SWTCH_OPEN : SWTCH_CLOSE;
                break;
            case R.id.rg_motor_call_enable:
                mCallEnable = (checkedId == R.id.rb_call_open) ? SWTCH_OPEN : SWTCH_CLOSE;
                break;
            case R.id.rg_motor_message_enable:
                mMsgEnable = (checkedId == R.id.rb_msg_open) ? SWTCH_OPEN : SWTCH_CLOSE;
                break;
            case R.id.rg_motor_alarm_enable:
                mAlarmEnable = (checkedId == R.id.rb_alarm_open) ? SWTCH_OPEN : SWTCH_CLOSE;
                break;
            case R.id.rg_motor_lowpower_enable:
                mLowpowerEnable = (checkedId == R.id.rb_lowpower_open) ? SWTCH_OPEN : SWTCH_CLOSE;
                break;
            case R.id.rg_heart_enable:
                mHeartEnable = (checkedId == R.id.rb_heart_open) ? SWTCH_OPEN : SWTCH_CLOSE;
                break;
            default:
                break;
        }
    }

    private void showToast(String input) {
        Toast.makeText(this, input, Toast.LENGTH_SHORT).show();
    }
}
